package com.lanou.lazyman.welcomepage;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;

import com.lanou.lazyman.MainActivity;
import com.lanou.lazyman.R;
import com.lanou.lazyman.base.BaseActivity;


/**
 * Created by dllo on 16/3/2.
 */
public class WelcomeActivity extends BaseActivity {
    float startX = 0, endX = 0;

    private ViewFlipper viewFlipper_BackGround, viewFlipper_Btn;
    private Button jump_btn;

    private SharedPreferences sharedPreferences;


    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {


        viewFlipper_BackGround = BlindView(R.id.viewflipper_background);
        viewFlipper_Btn = BlindView(R.id.viewflipper_btn);
        jump_btn = BlindView(R.id.jump_btn);


        sharedPreferences = getSharedPreferences("welcome", MODE_PRIVATE);
//        if(sharedPreferences.getBoolean("isFirst",true)){
//            SharedPreferences.Editor editor=sharedPreferences.edit();
//            editor.putBoolean("isFirst", false);
//            editor.commit();
//        }else{
//            Intent intent=new Intent(WelcomeActivity.this,GuideActivity.class);
//            startActivity(intent);
//            finish();
//        }

        //直接调用ViewFlipper的startFlipping()方法，开始滑动
        viewFlipper_Btn.startFlipping();
        SetLeftAnimation();
        viewFlipper_BackGround.startFlipping();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    Thread.sleep(12000);
                    viewFlipper_BackGround.stopFlipping();
                    viewFlipper_Btn.stopFlipping();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //  viewFlipper_BackGround.stopFlipping();

        viewFlipper_BackGround.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // ViewFlipper::showNext();  //显示下一个视图
                // ViewFlipper::showPrevious(); //显示上一个视图
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        endX = event.getX();
                        if (startX - endX > 0) {
                            //左滑
                            viewFlipper_Btn.showNext();
                            SetLeftAnimation();
                            viewFlipper_BackGround.showNext();
                        } else if (startX == endX) {

                        } else {
                            //右滑
                            viewFlipper_Btn.showPrevious();
                            SetRigthtAnimation();
                            viewFlipper_BackGround.showPrevious();
                        }
                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        return false;
                }

                return true;

            }
        });

        jump_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WelcomeActivity.this, GuideActivity.class);
                startActivity(intent);
                finish();
            }
        });


    }

    @Override
    protected int setcontent() {
        return R.layout.activity_welcome;
    }

    public void SetLeftAnimation() {
        viewFlipper_BackGround.setInAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_left_in));
        viewFlipper_BackGround.setOutAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_left_out));

    }

    public void SetRigthtAnimation() {
        viewFlipper_BackGround.setInAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_right_in));
        viewFlipper_BackGround.setOutAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_right_out));

    }


}
